﻿namespace WPFDBBrowser.Settings
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Xml.Serialization;
    using CustomsExplorer.Helpers;
    using WPFDBBrowser.Helpers;
    using System.ComponentModel;
    using WPFDBBrowser.Interfaces.DBTreeView;
    using WPFDBBrowser.Interfaces;

    public class MasterDBSettings : SettingsBase<List<DBConnectionInfo>>
    {
        private static readonly string WPFDBBrowserDataDir = "WPFDBBrowser";
        private static readonly string WPFDBBrowserPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), WPFDBBrowserDataDir);

        public MasterDBSettings()
            : base(Path.Combine(WPFDBBrowserPath, SettingsFileNames.MASTER_CONNECTIONS_FILE))
        {
            this.DBsInfos = new List<DBConnectionInfo>();
        }

        [XmlElement("Connections")]
        public List<DBConnectionInfo> DBsInfos { get; set; }

        public override bool SaveSettings()
        {
            if (!Directory.Exists(WPFDBBrowserPath))
                Directory.CreateDirectory(WPFDBBrowserPath);
            base.Save(DBsInfos);
            return true;
        }

        public override bool LoadSettings()
        {
            object obj = base.Load();
            if (obj == null)
                return false;
            this.DBsInfos = (List<DBConnectionInfo>)obj;
            return true;
        }
    }

    [XmlRoot("DBConnection")]
    public class DBConnectionInfo : INotifyPropertyChanged
    {
        private string _connectionString;
        private string _name;
        private string _type;

        public DBConnectionInfo()
        {
        }

        public DBConnectionInfo(string type)
            : this(null, null, type)
        {
        }

        public DBConnectionInfo(string connectionString, string name, string type)
        {
            this.ConnectionString = connectionString;
            this.Name = name;
            this.Type = type;
        }

        [XmlAttribute("connectionString")]
        public string ConnectionString
        {
            get
            {
                return this._connectionString;
            }
            set
            {
                if (string.IsNullOrEmpty(this._connectionString) &&
                    string.IsNullOrEmpty(value))
                    return;
                if (string.IsNullOrEmpty(this._connectionString) || !this._connectionString.Equals(value))
                {
                    this._connectionString = value;
                    this.OnPropertyChanged("ConnectionString");
                }
            }
        }
        [XmlAttribute("name")]
        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                if (string.IsNullOrEmpty(this._name) &&
                    string.IsNullOrEmpty(value))
                    return;
                if (string.IsNullOrEmpty(this._name) || !this._name.Equals(value))
                {
                    this._name = value;
                    this.OnPropertyChanged("Name");
                }
            }
        }
        [XmlAttribute("type")]
        public string Type
        {
            get
            {
                return this._type;
            }
            set
            {
                if (string.IsNullOrEmpty(this._type) &&
                    string.IsNullOrEmpty(value))
                    return;
                if (string.IsNullOrEmpty(this._type) || !this._type.Equals(value))
                {
                    this._type = value;
                    this.OnPropertyChanged("Type");
                }
            }
        }

        public IDBFactory DBFactory
        {
            get
            {
                if (!string.IsNullOrEmpty(this.Type))
                    return UnityContainerFactory.GetInstance().Resolve<IDBFactory>(this.Type);
                return null;
            }
        }

        public override string ToString()
        {
            return Name;
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
